package com.bizmerce.ebms.engine;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

public class OutBoundProcessor {

	public OutBoundProcessor() {
		
	}
	
	public static void main(String args[]) {
		
		try {
			
			// SOAP Message 생성
			MessageFactory mf       = MessageFactory.newInstance();
			SOAPMessage soapMessage = mf.createMessage();
			
			// SOAP Envelope 생성
		    SOAPPart     soapPart     = soapMessage.getSOAPPart();
		    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
		    
		    soapEnvelope.setPrefix("SOAP");
		    
		    SOAPHeader   soapHeader   = soapEnvelope.getHeader();
		    
		    soapHeader.setPrefix("SOAP");
		    
		    SOAPBody     soapBody     = soapEnvelope.getBody();
		    
		    soapBody.setPrefix("SOAP");
		     
		    //네임스페이스 지정
		    soapEnvelope.addNamespaceDeclaration("SOAP",  "http://schemas.xmlsoap.org/soap/envelope/");
		    soapEnvelope.addNamespaceDeclaration("xlink", "http://www.w3.org/1999/xlink");
		    soapEnvelope.addNamespaceDeclaration("xsi",   "http://www.w3.org/2001/XMLSchema-instance");
		     
		    // SOAPFactory 생성
		    SOAPFactory soapFactory = SOAPFactory.newInstance();
		     
		    // SOAPHeader 생성
	        SOAPHeader header = soapEnvelope.getHeader();
	
	        header.addNamespaceDeclaration("eb", "http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd");
	        Name schemaLocName = soapFactory.createName("schemaLocation", "xsi", "http://www.w3.org/2001/XMLSchema-instance");
	        
	        header.addAttribute(schemaLocName, "http://schemas.xmlsoap.org/soap/envelope/ http://www.oasis-open.org/committees/ebxml-msg/schema/envelope.xsd");
	        
	        SOAPElement messageHeader = header.addChildElement(new QName("http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd", "MessageHeader", "eb"));
	        
	        messageHeader.setAttribute("eb:id", "MessageHeader");
	        messageHeader.setAttribute("eb:version", "2.0");
	        
	        // eb:From
	        SOAPElement from = messageHeader.addChildElement(new QName("http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd", "From", "eb"));
	        
	        // eb:PartyId
	        SOAPElement PartyId = from.addChildElement(new QName("http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd", "PartyId", "eb"));
	        PartyId.setAttribute("eb:type", "ok-customs.dtm1");
	        PartyId.addTextNode("VC402815416601");
	        
	        // eb:Role
	        SOAPElement role = from.addChildElement(new QName("http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd", "Role", "eb"));
	        role.addTextNode("http://www.ok-customs.go.kr/ebMSH#buyer");

	        soapMessage.writeTo(System.out);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
			
		}
		
		
	}
	
}
